package com.building.hcrm.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.building.hcrm.R;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.User;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.ui.customer.CustomerMainActivity;

public class MainActivity extends BaseActivity
{
	private ImageButton imgbtnCenter;
	private TextView _txtCenter;
	private ImageButton imgbtnOther;
	
	private ImageButton _imgBtnRe;
	private TextView _txtTitle;
	private ImageButton _imgBtnTitleRight;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InitTitle();

		imgbtnCenter = (ImageButton) findViewById(R.id.imgbtn_per_center);
		_txtCenter = (TextView)findViewById(R.id.txt_center);
		imgbtnOther = (ImageButton) findViewById(R.id.imgbtn_other);
		imgbtnCenter.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if(!User.getInstance(MainActivity.this).getIsLogin())
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), LoginActivity.class);
					startActivityForResult(intent, ComConst.REQUEST_LOGIN);
				}
				else
				{
					Intent intent = null;
					intent = new Intent(getApplicationContext(), SpaceActivity.class);
					intent.putExtra("Zone", "Person");
					startActivity(intent);
				}
			}
		});
		imgbtnOther.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = null;
				intent = new Intent(getApplicationContext(), SpaceActivity.class);
				intent.putExtra("Zone", "Other");
				startActivity(intent);
			}
		});
	}
	
	private void InitTitle()
	{
		_imgBtnRe = (ImageButton) findViewById(R.id.imgbtn_home);
		_imgBtnRe.setVisibility(View.INVISIBLE);
		_txtTitle = (TextView) findViewById(R.id.titlePageName);
		_txtTitle.setText(R.string.main_title);
		_imgBtnTitleRight = (ImageButton) findViewById(R.id.imgbtn_save);
		_imgBtnTitleRight.setVisibility(View.INVISIBLE);
	}
	

	private Toast _toast = null;
	long _startTime;
	long _endTime;

	@Override
	public void onBackPressed()
	{
		if (_toast != null)
		{
			_endTime = System.currentTimeMillis();
			if (_endTime - _startTime > 2500)
			{
				_toast = null;
			}
			else
			{
				super.onBackPressed();
			}
		}
		if (_toast == null)
		{
			_startTime = System.currentTimeMillis();
			_toast = Toast.makeText(MainActivity.this, R.string.app_exit_again_msg, Toast.LENGTH_SHORT);
			_toast.show();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO 自动生成的方法存根
		super.onActivityResult(requestCode, resultCode, data);
		try
		{
			if (data != null)
			{
				if (requestCode == ComConst.REQUEST_LOGIN)
				{
					if (resultCode == ComConst.RESULT_LOGIN)
					{
						Boolean bIsLogin = data.getBooleanExtra(ComConst.RESULT_BACK, false);
						if (bIsLogin)
						{
							SetBottomStatus();
						}
					}
				}
				else if (requestCode == ComConst.REQUEST_SCANNING)
				{
					if (resultCode == ComConst.RESULT_SCANNING)
					{
						String str = data.getStringExtra(ComConst.RESULT_BACK);
						if (str != null && str.length() > 0)
						{
							
						}
					}
				}
			}
		}
		catch (Exception e)
		{
		}
	}

	private void SetBottomStatus()
	{
		if(User.getInstance(MainActivity.this).getIsLogin())
		{
			imgbtnCenter.setImageDrawable(getResources().getDrawable(R.drawable.imgbtn_percenter_fore_selector));
			_txtCenter.setVisibility(View.VISIBLE);
			imgbtnOther.setVisibility(View.VISIBLE);
		}
		else
		{
			imgbtnCenter.setImageDrawable(getResources().getDrawable(R.drawable.imgbtn_no_login_fore_selector));
			_txtCenter.setVisibility(View.GONE);
			imgbtnOther.setVisibility(View.GONE);
		}
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		SetBottomStatus();
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		getHcrmApp().finishActivity();
		System.exit(0);
	}
}
